package com.example.java250729_springboot.controller;


import com.example.java250729_springboot.entity.Person;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {
    @RequestMapping("/r1")
    public String r1(String name){
        return "接收到参数 name = " + name;
    }
    @RequestMapping("/r2")
    public String r2(Integer age){
        return "接收到的参数 age = "+age;
    }
    @RequestMapping("/r3")
    public String r3(int age){
        //基础类型不能设置为null
        return "接收到的参数 age = "+age;
    }
    @RequestMapping("/r4")
    public String r4(String name,String password,Integer age){
        //此处字符串拼接，不考虑性能
        return "接收到参数 name： " + name + ", password:" + password + ",age:" + age;
    }
    @RequestMapping("/r5")
    public String r5(Person person){
        return "接收到的参数 person" + person;
    }

    @RequestMapping("/r6")
    public String r6(@RequestParam(value = "sa",required = false) String resource){
        return "接收到参数 resource" + resource;
    }

    @RequestMapping("/r7")
    public String r7(String[] names){
        return "接收到参数 names: "+ Arrays.toString(names);
    }
    @RequestMapping("/r8")
    public String r8(@RequestParam List<String> names){
        return "接收到参数 name: " + names;
    }

    @RequestMapping("/r9")
    public String r9(@RequestBody Person person){
        return "接收到参数 person:" + person;
    }

    /**
     * 获取url中的参数
     * @param articleId
     * @return
     */
    @RequestMapping("/r10/{articleId}")
    public String r10(@PathVariable Integer articleId){
        return "接收到的参数person：" + articleId;
    }

    @RequestMapping("/r11/{articleId}/{type}")
    public String r11(@PathVariable("articleId") Integer id,@PathVariable String type){
        return "接收到的参数p：articleId" + id + ",type:" + type;
    }

    /**
     * 接收文件
     */
    @RequestMapping("/r12")
    public String r12(@RequestPart("file1")MultipartFile file) throws IOException {
        System.out.println(file.getName());
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getContentType());
        File file1 = new File("D:/temp/"+file.getOriginalFilename());
        file.transferTo(file1);
        return "接收到的文件";
    }
}





















